package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.view.View;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.sgcc.sx.pmos.arouter.mall.MallArouterPath;
import com.sgcc.sx.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.sx.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.sx.pmos.common.base.BaseFragment;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.sx.pmos.common.utils.VisibilConfig;
import com.sgcc.pmos.nativesdk.R;

import java.util.ArrayList;
import java.util.List;

public class MallFragment extends BaseFragment {

    private SlidingTabLayout mStlContent;
    private ViewPager mViewpager;
    private RelativeLayout mRlSearch;
    private RelativeLayout mRlMessage;
    private final List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_mall;
    }

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mStlContent = mRootView.findViewById(R.id.stl_content);
        mViewpager = mRootView.findViewById(R.id.viewpager);

        mRlSearch = mRootView.findViewById(R.id.rl_search);
        mRlMessage = mRootView.findViewById(R.id.rl_message);

        String[] titleList = getResources().getStringArray(R.array.mall_title);

        GoodsFragment goodsFragment = new GoodsFragment();
        StoreFragment storeFragment = new StoreFragment();

        fragments.add(goodsFragment);
        fragments.add(storeFragment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getActivity().getSupportFragmentManager(), fragments);
        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mViewpager.setOffscreenPageLimit(2);
        mViewpager.setAdapter(homeTabPagerAdapter);
        mStlContent.setViewPager(mViewpager);
        if (!VisibilConfig.isShowVisibil()) {
            mRlMessage.setVisibility(View.INVISIBLE);
        }
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mRlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_SEARCH)
                        .withInt("searchType", mViewpager.getCurrentItem())
                        .navigation();
            }
        });
        mRlMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showShort("敬请期待");
//                ARouter.getInstance()
//                        .build(PersonalCenterArouterPath.PERSONALCENTER_MESSAGE)
//                        .navigation();
            }
        });
    }

    @Override
    public void onEventMainThread(EventCenter eventCenter) {
        super.onEventMainThread(eventCenter);
        switch (eventCenter.getEventCode()) {

            case ArguConstant.GO_MALL_STORE_OR_GOODS:
                Integer data = (Integer) eventCenter.getData();
                mViewpager.setCurrentItem(data);
                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        int mallPage = SPUtils.getInstance().getInt("mallPage");
        if (mallPage == 1) {
//            mViewpager.setCurrentItem(0);
            mStlContent.setCurrentTab(0);
            SPUtils.getInstance().remove("mallPage");
        } else if (mallPage == 2) {
//            mViewpager.setCurrentItem(1);
            SPUtils.getInstance().remove("mallPage");
            mStlContent.setCurrentTab(1);
        }
    }
}
